import { reqGetCategory1List, reqGetCategory2List, reqGetCategory3List } from '@/api/product/category'
const state = {
  category1Id: '',
  category2Id: '',
  category3Id: '',
  category1List: [],
  category2List: [],
  category3List: []
}

const actions = {
  // 发送请求获取一级分类数据
  async GetCategory1List({ commit }) {
    const res = await reqGetCategory1List()
    if (res.code === 200) {
      commit('SAVE_CATEGORY1_LIST', res.data)
    } else {
      console.log(res.message)
    }
  },
  // 发送请求获取二级分类数据
  async GetCategory2List({ commit }, category1Id) {
    const res = await reqGetCategory2List(category1Id)
    if (res.code === 200) {
      commit('SAVE_CATEGORY2_LIST', { Category2List: res.data, category1Id })
    } else {
      console.log(res.message)
    }
  },
  // 发送请求获取三级分类数据
  async GetCategory3List({ commit }, category2Id) {
    const res = await reqGetCategory3List(category2Id)
    if (res.code === 200) {
      commit('SAVE_CATEGORY3_LIST', { Category3List: res.data, category2Id })
    } else {
      console.log(res.message)
    }
  }
}

const mutations = {
  // 存储一级分类数据
  SAVE_CATEGORY1_LIST(state, Category1List) {
    state.category1List = Category1List
  },
  // 存储二级分类数据
  SAVE_CATEGORY2_LIST(state, { Category2List, category1Id }) {
    state.category2List = Category2List
    state.category1Id = category1Id

    // 清空category2Id category3Id
    state.category2Id = ''
    state.category3Id = ''
    // 清空category3List
    state.Category3List = []
  },
  // 存储三级分类数据
  SAVE_CATEGORY3_LIST(state, { Category3List, category2Id }) {
    state.category3List = Category3List
    state.category2Id = category2Id

    // 清空category3Id
    state.category3Id = ''
  },
  // 存储三级Id
  SAVE_CATEGORY3ID(state, category3Id) {
    state.category3Id = category3Id
  },
  // 清空数据
  RESET(state) {
    state.category1Id = ''
    state.category2Id = ''
    state.category3Id = ''
    state.category2List = ''
    state.category3List = ''
  }
}

export default {
  namespaced: true,
  state,
  actions,
  mutations
}
